cd "C:\Users\marku\Dropbox\Fiscal Consolidation 3\Papers\Paper 3\CPS\replication\"
cd ...

log using mainpaper.txt, replace

*** POOLED RESULTS 
 
use "IMF_Greece.dta", clear

gen country=1

append using "IMF_Portugal.dta"
replace country=2 if country==. 
 append using "IMF_Spain.dta"
 replace country=3 if country==. 
append using  "IMF_Ireland.dta"
replace country=4 if country==. 

label define country 1 "GR" 2 "PT" 3 "ES" 4 "IE"
label values country country



local othtreat "i.rndm_publicdebt i.rndm_party i.rndm_spendingcuts"
local controls "Q2_2 Q2_3 Q3_5 i.pubsec i.inc5 ib3.govmatch i.isced4 econideology nattrust inttrust i.rndm_party##ib3.govmatch  i.country"  
local controls_withoutcountry "Q2_2 Q2_3 Q3_5 i.pubsec i.inc5 ib3.govmatch i.isced4 econideology nattrust inttrust i.rndm_party##ib3.govmatch"
local controls_nolr "Q2_2 Q2_3  i.pubsec i.inc5 ib3.govmatch i.isced4 econideology nattrust inttrust i.rndm_party##ib3.govmatch  i.country"
local controls_nolr_withoutcountry "Q2_2 Q2_3  i.pubsec i.inc5 ib3.govmatch i.isced4 econideology nattrust inttrust i.rndm_party##ib3.govmatch "

recode IMFM_2 (1=2) (2=1), gen (imfcheck)
label define imfcheck 1 "No" 2 "Yes", replace
label values imfcheck imfcheck
gen main_rad=.
replace main_rad=0 if vote_family==1|vote_family==4
replace main_rad=1 if vote_family==2|vote_family==6
replace main_rad=2 if vote_family==3|vote_family==7

label define main_rad 0 "Mainstream" 1 "Radical", replace
label values main_rad main_rad
label variable main_rad "Party vote"

tab inc5, gen(inccat)
tab govmatch, gen(govmatchcat)
tab isced4, gen(iscedcat)
tab country, gen(countrycat)
gen inteffect1=rndm_party*govmatchcat1
gen inteffect2=rndm_party*govmatchcat2

*** FIGURE 4: Distribution of IMF conditionality across different levels of voter approval for austerity package
twoway (histogram IMFQ1_1 if imf==0, start(0) width(1) discrete percent color(gray%50)) ///        
       	   (histogram IMFQ1_1 if imf==1, start(0) width(1) discrete percent color(red%30)) , ///   
       legend(order( 1 "No IMF"  2 "IMF") rows(1) position(6)) scheme(plotplain) saving(1.gph, replace)
	   
	twoway (histogram IMFQ1_1 if imf==0, start(0) width(1) discrete percent color(gray%50) by(country, note(""))) /// 
	(histogram IMFQ1_1 if imf==1, start(0) width(1) discrete percent color(red%30) by(country, note(""))), /// 
       legend(order(1 "No IMF"  2 "IMF" ) rows(1)) scheme(plotplain)   saving(2.gph, replace)
	  
graph combine 1.gph 2.gph, scheme(plotplain) title("Treatment effect: IMF involvement")

graph export Figure4.png, replace width(3600)



* Figure 5: Effect of IMF conditionality on voter approval for austerity package
reg IMFQ1_1 imf `othtreat' `controls'
estimates store m0
forvalues x = 1/4 {
reg IMFQ1_1 imf `othtreat' `controls_withoutcountry' if country==`x'
estimates store m`x'
}
coefplot m0 m1 m2 m3 m4, ///
keep(imf) ///
scheme(plotplain) ///
xline(0, lpattern(dash)) yline(.75, lpattern(solid)) title("IMF treatment on approval")  levels(99 95) legend(off) ///
msymbol(O) mcolor(black)  ///
xtitle("<<< lower approval || higher approval >>>") ///
 ylabel(.66 "Overall" .83 "Greece" 1 "Portugal" 1.17 "Spain" 1.34 "Ireland") yscale(range(.5 1.5)) note("Note: 99% and 95% CIs shown", size(vsmall))
graph export Figure5.png, replace width(3600)





***Figure 6: Treatment effects, by voter-level characteristics
* By LR ideology
reg IMFQ1_1 i.imf##c.Q3_5    i.imf##c.inttrust  `othtreat' `controls_nolr'
margins, dydx(imf) at(Q3_5=(0(1)10))
marginsplot, scheme(plotplain) recastci(rline) ciopts(lpattern(dash_dot)) recast(line) ///
 title(HTE by Left-Right ideology)  xtitle("Left-right ideology") ytitle("Treatment effect",height(-10)) yline(0, lpattern(dash)) aspectratio(0.5) ///
 addplot(hist Q3_5,  yaxis(2) bin(11) percent yscale(range(0 100) axis(2)) ylabel("", axis(2) ) ylabel(-.5 (.5) 1)  yscale(range(-.5 1) axis(1)) ytitle("", axis(2))) xscale(range(-.5 10.5)) xlabel(0 "0" 1 "1" 3 "3" 5"5" 7"7" 9"9" 10) legend(off) saving(0a.gph, replace)
 * By economic ideology
 reg IMFQ1_1  i.imf##c.econideology  i.imf##c.inttrust  `othtreat' `controls_nolr'
margins, dydx(imf) at(econideology=(1(.5)5))
marginsplot, scheme(plotplain) recastci(rline) ciopts(lpattern(dash_dot)) recast(line) ///
 title(HTE by economic ideology) xtitle("Economic ideology") ytitle("Treatment effect") aspectratio(0.5) yline(0, lpattern(dash))  ///
 addplot(hist econideology, bin(11) yaxis(2) percent yscale(range(0 100) axis(2)) ylabel("", axis(2)) ylabel(-.5 (.5) 1)  yscale(range(-.5 1) axis(1)) ytitle("", axis(2))) legend(off) saving(0b.gph, replace)
 * By trust in international insts
reg IMFQ1_1 i.imf##c.econideology  i.imf##c.inttrust   `othtreat' `controls'
margins, dydx(imf) at(inttrust=(0(1)10))
marginsplot, scheme(plotplain) recastci(rline) ciopts(lpattern(dash_dot)) recast(line) ///
 title(HTE by trust in int'l institutions) xtitle("Trust in international institutions") aspectratio(0.5)   ///
 ytitle("Treatment effect") yline(0, lpattern(dash)) ///
 addplot(hist inttrust,  yaxis(2) bin(11) percent yscale(range(0 100) axis(2)) ylabel("", axis(2)) ylabel(-.5 (.5) 1)  yscale(range(-.5 1) axis(1)) ytitle("", axis(2)))  legend(off) saving(0c.gph, replace)
 * By government support
reg IMFQ1_1 i.imf##i.govmatch `othtreat' `controls' 
margins, dydx(imf) at(govmatch=(1 2 3))
marginsplot, scheme(plotplain) recastci(rcap) ciopts(lpattern(dash_dot)) recast(scatter) ///
 title(HTE by government support) ytitle("Treatment effect") yline(0, lpattern(dash)) ///
 addplot(hist govmatch, fcolor(%30) discrete yaxis(2) yscale(range(-.8 .8) axis(1)) ylabel(-.6 (.2).6, axis(1)) xscale(range(0 4)) percent yscale(range(0 200) axis(2)) ylabel("", axis(2)) ytitle("", axis(2)) xlabel(1 "Gov voter" 2 "Opp voter" 3 "Neither") xtitle(Gov support) )  legend(off) saving(0.gph, replace)
 * By type of party supported
 reg IMFQ1_1 i.imf##i.main_rad i.imf##i.govmatch `othtreat' `controls' if country!=4 
margins, dydx(imf) at(main_rad=(0 1))
marginsplot, scheme(plotplain) recastci(rcap) ciopts(lpattern(dash_dot)) recast(scatter) ///
 title(HTE by type of party supported) ytitle("Treatment effect") yline(0, lpattern(dash)) yscale(range(-.6 .8)) xlabel(0 "Mainstream" 1 "Radical") ///
 legend(off)  saving(0d.gph, replace) ///
 addplot(hist main_rad if main_rad<2, fcolor(%30) discrete yaxis(2) percent yscale(range(50 200) axis(2)) yscale(range(-.8 .8) axis(1)) ylabel(-.6 (.2).6, axis(1)) xscale(range(-1 2)) xlabel(0 "Mainstream" 1 "Radical") ylabel("", axis(2)) ytitle("", axis(2))) 
 
graph combine 0a.gph 0b.gph 0c.gph , saving(HTE.gph, replace) scheme(plotplain) row(1)
graph combine  0.gph 0d.gph , saving(HTE_vote.gph, replace) scheme(plotplain) row(1) holes(3)
graph combine HTE.gph HTE_vote.gph, scheme(plotplain) row(2) 
graph export Figure6.png, replace  width(3600) 


*** Figure 7: Treatment effects on mediators, by country

local othtreat "rndm_publicdebt rndm_party rndm_spendingcuts"
local controls "Q2_2 Q2_3 Q3_5 i.pubsec i.inc5 ib3.govmatch i.isced4 econideology nattrust inttrust i.rndm_party##ib3.govmatch  i.country"  
local controls_withoutcountry "Q2_2 Q2_3 Q3_5 i.pubsec i.inc5 ib3.govmatch i.isced4 econideology nattrust inttrust i.rndm_party##ib3.govmatch"


reg IMFQ2_2 imf `othtreat' `controls'
estimates store m0
forvalues x = 1/4 {
reg IMFQ2_2 imf `othtreat' `controls_withoutcountry' if country==`x'
estimates store m`x'
}
coefplot m0 m1 m2 m3 m4, ///
keep(imf) ///
scheme(plotplain) ///
xline(0, lpattern(dash)) yline(.75, lpattern(solid)) xscale(range(-1 .7)) xlabel(-1(.2).7)  title("IMF treatment on effectiveness")  levels(99 95) legend(off) ///
msymbol(O) mcolor(black)  ///
xtitle("<<< less likely to succeed || more likely to succeed >>>") ///
 ylabel(.66 "Overall" .83 "Greece" 1 "Portugal" 1.17 "Spain" 1.34 "Ireland") yscale(range(.5 1.5)) saving(2.gph, replace)

reg IMFQ2_3 imf `othtreat' `controls'
estimates store m0
forvalues x = 1/4 {
reg IMFQ2_3 imf `othtreat' `controls_withoutcountry' if country==`x'
estimates store m`x'
}
coefplot m0 m1 m2 m3 m4, ///
keep(imf) ///
scheme(plotplain) ///
xline(0, lpattern(dash)) yline(.75, lpattern(solid)) xscale(range(-1 .7)) xlabel(-1(.2).7) title("IMF treatment on sovereignty")  levels(99 95) legend(off) ///
msymbol(O) mcolor(black)  ///
xtitle("<<< less freedom || more freedom >>>") ///
 ylabel(.66 "Overall" .83 "Greece" 1 "Portugal" 1.17 "Spain" 1.34 "Ireland") yscale(range(.5 1.5)) saving(3.gph, replace)

graph combine  2.gph 3.gph  , row(1) scheme(plotplain) note("Note: 99% and 95% CIs shown", size(vsmall))
graph export Figure7.png, replace width(3600)

*** Figure 8: ACME for each mechanism, by country

local othtreat "rndm_publicdebt rndm_party rndm_spendingcuts"
local controls "Q2_2 Q2_3 Q3_5 pubsec  govmatchcat1-govmatchcat2 iscedcat1-iscedcat3 econideology nattrust inttrust inteffect1 inteffect2 countrycat1-countrycat3"  
local controls_withoutcountry "Q2_2 Q2_3 Q3_5 pubsec  govmatchcat1-govmatchcat2 iscedcat1-iscedcat3 econideology nattrust inttrust inteffect1 inteffect2 "
local mediators "IMFQ2_1 IMFQ2_2 IMFQ2_3 IMFQ2_4"

forvalues x = 1/4 {
medeff (regress IMFQ2_`x' imf  `othtreat' `controls') (regress IMFQ1_1 IMFQ2_`x' imf  `othtreat' `controls'), mediate(IMFQ2_`x') treat(imf) sims(2000) seed(12345)
matrix a`x'=`x', r(delta0), r(delta0lo), r(delta0hi) , r(zeta0), r(zeta0lo), r(zeta0hi) , r(tau), r(taulo), r(tauhi)
*medsens (regress IMFQ2_`x' imf  `othtreat' `controls') (regress IMFQ1_1 IMFQ2_`x' imf  `othtreat' `controls'), mediate(IMFQ2_`x') treat(imf) sims(2000) 
*by country
forvalues y = 1/4 {
preserve
drop if country!=`y'	
medeff (regress IMFQ2_`x' imf  `othtreat' `controls_withoutcountry') (regress IMFQ1_1 IMFQ2_`x' imf `othtreat' `controls_withoutcountry'), mediate(IMFQ2_`x') treat(imf) sims(2000) seed(12345)
matrix b`x'`y'=`y', r(delta0), r(delta0lo), r(delta0hi) , r(zeta0), r(zeta0lo), r(zeta0hi) , r(tau), r(taulo), r(tauhi)

*medsens (regress IMFQ2_`x' imf `othtreat' `controls_withoutcountry') (regress IMFQ1_1 IMFQ2_`x' imf `othtreat' `controls_withoutcountry'), mediate(IMFQ2_`x') treat(imf) sims(2000) 

restore
}
}

matrix r=a1\a2\a3\a4
svmat r

label define mediator 1 "Competence" 2 "Effectiveness" 3 "Sovereignty" 4 "Accountability", replace
label values r1 mediator

matrix succ=a2\b21\b22\b23\b24
svmat succ
replace succ1=_n if succ1!=.
graph twoway (scatter  succ1 succ2) (rcap  succ3 succ4 succ1, horizontal), scheme(plotplain) ///
legend(off) title("Effectiveness") ytitle("") yline(1.5, lpattern(dash)) ylabel(1 "Overall" 2 "Greece" 3 "Portugal" 4 "Spain" 5 "Ireland") ///
xline(0, lpattern(dash)) xscale(range(-.35 .35)) xlabel(-.35(.1).35) ysc(reverse) saving(2.gph, replace)

matrix room=a3\b31\b32\b33\b34
svmat room
replace room1=_n if room1!=.

graph twoway (scatter  room1 room2) (rcap  room3 room4 room1, horizontal), scheme(plotplain) ///
legend(off) title("Sovereignty") ytitle("") yline(1.5, lpattern(dash)) ylabel(1 "Overall" 2 "Greece" 3 "Portugal" 4 "Spain" 5 "Ireland") ///
xline(0, lpattern(dash)) xscale(range(-.35 .35)) xlabel(-.35(.1).35) ysc(reverse) saving(3.gph, replace)


graph combine  2.gph 3.gph , row(1) scheme(plotplain) note("Note: 95% CIs shown", size(vsmall))
graph export Figure8.png, replace width(3600)

log close








